// src/api/UserApi.ts
import { ApiResult } from "../model/ApiResult";
import { User } from "../model/User";
import { Get, Post } from "../http/HttpDecorators";
// import { MyMock } from "../mock/mock";

import { MyMock } from "../mock/MyMock";
import { http } from "@kit.NetworkKit";
import { BusinessError } from "@kit.BasicServicesKit";

export class UserApi {
    @MyMock
    @Post
    public static async login(username: string, password: string): Promise<boolean> {
        let httpRequest = http.createHttp();
        try {
            const response = await new Promise<{ err: BusinessError; data: http.HttpResponse }>((resolve) => {
                httpRequest.request(
                    "http://10.83.55.146:8080/api/auth/login",
                    {
                        method: http.RequestMethod.POST,
                        extraData: JSON.stringify({
                            "username": username,
                            "password": password
                        }),
                        expectDataType: http.HttpDataType.STRING,
                        header: { 'Content-Type': 'application/json' },
                        readTimeout: 60000,
                        connectTimeout: 60000,
                    },
                    (err: BusinessError, data: http.HttpResponse) => {
                        resolve({ err, data });
                    }
                );
            });

            if (response.err) {
                console.error('Login error:', response.err);
                httpRequest.destroy();
                return false;
            }

            console.info('Result:' + response.data.result);
            let res = JSON.parse(response.data.result as string) as ApiResult<User>;
            console.info("code = " + res.code);
            return res.code === 20000;
        } catch (error) {
            console.error('Unexpected error:', error);
            httpRequest.destroy();
            return false;
        }
    }

    @Get
    public static async getUser(userId: number): Promise<ApiResult<User>> {
        // 实际代码会被装饰器替换
        return null as any;
    }
}